﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using LsoaClass;
using System.Text;

public partial class Main : LsPage
{
    public string MAIN_BLOCK_json = "";
    public string SUBMENU_CONFIG_json = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //绑定顶部菜单类型
            bindMenuType();

            //绑定菜单列表
            bindMenu();
        }
    }

    //绑定顶部菜单类型
    private void bindMenuType()
    {
        LsoaClass.SystemMaintain.Menu m = new LsoaClass.SystemMaintain.Menu();
        result.RetStr = m.getMenuTypeList(out outLsDs);
        if (result.Code == 0)
        {
            //1.帮顶部图片列表
            rpMenuType.DataSource = outLsDs.Table;
            rpMenuType.DataBind();

            //2.显示第一个标题
            if (outLsDs.read())
            {
                ltFirstMenuType.Text = outLsDs["Name"].ToString();
            }

            //3.生成MAIN_BLOCK_json串，形式：MAIN_BLOCK_json = [{id:'initiator',name:'新闻'},{id:'config',name:'全局'}]
            outLsDs.first();
            List<string> MAIN_BLOCK_List = new List<string>();
            while (outLsDs.read())
            {
                MAIN_BLOCK_List.Add(String.Format("{{id:'{0}',name:'{1}'}}", outLsDs["Keyword"].ToString(), outLsDs["Name"].ToString()));
            }
            MAIN_BLOCK_json = "[" + String.Join(",", MAIN_BLOCK_List.ToArray()) + "]";
        }
        else
        {
            Response.Write(result.Msg);
        }
    }

    #region 绑定二级菜单
    /*生成形式
      {'initiator':{'id':'','items':[{id:'manager',name:'新闻列表',url:'News/NewsList.aspx'},
                                    {'id':'usercenter','name':'用户中心','items':[{id:'ucset',name:'用户中心设置',url:'/phpwind/admin.php?adminjob=ucset'},                                                                                  
                                                                                  {id:'ucnotify',name:'消息队列',url:'/phpwind/admin.php?adminjob=ucnotify'}]}]},
      'config':{'id':'','items':[{id:'basic',name:'站点设置',url:'/phpwind/admin.php?adminjob=settings&admintype=basic'},                                 
                                 {'id':'regset','name':'注册设置','items':[{id:'reg',name:'注册控制',url:'/phpwind/admin.php?adminjob=settings&admintype=reg'},                                                                           
                                                                           {id:'invite',name:'邀请注册',url:'/phpwind/admin.php?adminjob=friend&admintype=invite'}]}
       ｝
     **/
    private void bindMenu()
    {
        LsoaClass.SystemMaintain.Menu m = new LsoaClass.SystemMaintain.Menu();
        result.RetStr = m.getMenuList(out outLsDs);
        if (result.Code == 0)
        {
            StringBuilder totalStr = new StringBuilder();
            DataView dv = outLsDs.Table.DefaultView;

            //获取大类，相当于distinct一下keyword
            List<string> menuType = new List<string>();
            for (int i = 0; i < dv.Count; i++)
            {
                if(!menuType.Contains(dv[i]["keyword"].ToString()))
                {
                    menuType.Add(dv[i]["keyword"].ToString());
                }
            }
            
            //获取大类的串
            totalStr.Append("{");
            for(int i = 0; i < menuType.Count; i++)
            {
                totalStr.Append(getMenuJsonByKeyWord(menuType[i], dv));
                if (i != menuType.Count - 1)
                {
                    totalStr.Append(",");
                }
            }
            totalStr.Append("}");

            //绑定到前台
            SUBMENU_CONFIG_json = totalStr.ToString();
        }
        else
        {
            Response.Write(result.Msg);
        }
    }

    //生成某一大类的串，形式如：'initiator':{'id':'','items':[{id:'manager',name:'新闻列表',url:'News/NewsList.aspx'}
    private string getMenuJsonByKeyWord(string keyword, DataView dv)
    {
        StringBuilder sb = new StringBuilder();
        dv.RowFilter = "keyword='" + keyword + "' and ParentNo='0'";

        sb.Append("'" + keyword + "':{'id':'','items':[");
        for (int i = 0; i < dv.Count; i++)
        {
            if (dv[i]["MenuSub"].ToString() == "False") //没有子菜单
            {
                sb.Append(String.Format("{{id:'{0}',name:'{1}',url:'{2}'}}", dv[i]["MenuNo"], dv[i]["MenuName"], dv[i]["MenuUrl"]));               
            }
            else //有子菜单
            {
                sb.Append(String.Format("{{'id':'{0}','name':'{1}','items':[{2}]}}", dv[i]["MenuNo"], dv[i]["MenuName"], getSubMenuJsonByKeyMenuNo(dv[i]["MenuNo"].ToString(), dv.Table.Copy().DefaultView)));
            }

            if (i != dv.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");

        return sb.ToString();
    }

    //返回子菜单，形式如{id:'ucset',name:'用户中心设置',url:'/phpwind/admin.php?adminjob=ucset'}
    private string getSubMenuJsonByKeyMenuNo(string menuNo, DataView dv)
    {
        StringBuilder sb = new StringBuilder();
        dv.RowFilter = "ParentNo='" + menuNo + "'";        
        for (int i = 0; i < dv.Count; i++)
        {
            sb.Append(String.Format("{{id:'{0}',name:'{1}',url:'{2}'}}", dv[i]["MenuNo"], dv[i]["MenuName"], dv[i]["MenuUrl"]));
            if (i != dv.Count - 1)
            {
                sb.Append(",");
            }
        }        

        return sb.ToString();
    }
    #endregion

    //注销
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}
